package usernamepassword;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class UsernamePassword {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner keyboard = new Scanner(System.in);
        String input;
        int inputn;
        Map m = new Map();
        boolean check;
        String pw;
        File file = new File("UserPass.txt");

        Scanner scanFile = new Scanner(file);
        while (true) {
            System.out.println("Welcome to The UP Database");
            System.out.println("Please enter \"Collection\" to view the list of Usernames.");
            System.out.println("Please enter \"Lost\" if you have lost your Password");
            System.out.println("Please enter\"New\" to create a New Account in The Database");
            System.out.println("Please enter \"Search\" to search for a Username in The Database");
            System.out.println("Please enter \"export\" to export from The Database");
            System.out.println("Please enter \"import\" to import to The Database from a file");
            System.out.println("Please enter \"remove\" to remove a value from The Database");
            System.out.println("Please enter \"count\" to see the number of values in The Database");
            System.out.println("Please enter \"end\" to leave The Database... but why would you want to?");
            input = keyboard.nextLine();
            if (input.equalsIgnoreCase("collection")) {
                m.printID();
            } else if (input.equalsIgnoreCase("Lost")) {
                System.out.println("Please insert your username");
                inputn = keyboard.nextInt();
                m.printPW(inputn);
                keyboard.nextLine();
            } else if (input.equalsIgnoreCase("new")) {
                check = true;
                while (check) {
                    System.out.println("Insert your prefered username");
                    inputn = keyboard.nextInt();
                    if (!m.Contains(inputn)) {
                        Node n = new Node();
                        n.setId(inputn);
                        System.out.println("Insert your prefered password");
                        input = keyboard.nextLine();
                        n.setValue(input);
                        keyboard.nextLine();
                        System.out.println("Thank You");
                        m.nodes.add(n);
                        check = false;
                    } else {
                        System.out.println("I'm sorry, that user name exists");
                    }



                }

            } else if (input.equalsIgnoreCase("search")) {
                System.out.println("Insert the Username you'd like to search for");
                inputn = keyboard.nextInt();
                if (m.Contains(inputn)) {
                    System.out.println("That Username exists");
                    pw = (m.nodes.get(m.getArrayvalue()).getValue());
                } else {
                    System.out.println("That Username doesn't exist");
                }
                keyboard.nextLine();
            } else if (input.equalsIgnoreCase("end")) {

                System.exit(0);
            } else if (input.equalsIgnoreCase("export")) {
                PrintWriter prwr = new PrintWriter(file);
                prwr.println(m.export());
                prwr.close();
                System.out.println("File exported");
            } else if (input.equalsIgnoreCase("import")) {
                String[] imp;
                String splitter = ",";
                String t;
                while (scanFile.hasNextLine()) {
                    t = scanFile.nextLine();
                    imp = t.split(splitter);
                    Node n = new Node();
                    n.id = Integer.parseInt(imp[1]);
                    n.value = imp[0];
                    m.nodes.add(n);
                }
                System.out.println("File Imported");
            } else if (input.equalsIgnoreCase("remove"))
            {
                System.out.println("Insert the Username you'd like to remove");
                inputn = keyboard.nextInt();
                if (m.delete(inputn)) {
                    System.out.println("That Username exists and has been removed");
                    
                } 
                else {
                    System.out.println("That Username doesn't exist");
                }
            }
            else if (input.equalsIgnoreCase("count"))
            {
                System.out.println(m.count());
            }
            else {
                System.err.println("ERROR-NOT VALID ENTRY");
            }
        }
    }
}
